<?php

namespace app\user\controller;

class Mailbox extends base
{
    public function _initialize()
    {
        $this->model = model('mailbox');
    }

//前置方法
    protected $beforeActionList = [
        'map' => ['only' => 'index'],
    ];

    /**
     * 搜索
     */
    protected function map()
    {
        if ($this->request->isGet()) {
            $data = $this->request->param();
            $this->datas = array();

            if (!empty($data['title'])) {
                $this->datas ['title'] = ['like', '%' . $data['title'] . '%'];
            }
        }

        $this->datas ['uid'] = $this->getLoginUser()->id;
        $this->datas ['type'] = $data['type'];
    }


    public function view()
    {
        $one =$where =array();
        if($this->request->param('toid')){
            $where['toid'] = $this->getLoginUser()->id;
        }else{
            $where['uid'] = $this->getLoginUser()->id;

        }

        $one['one'] = $this->model->where('type',1)->where($where)->count();
        $one['two'] = $this->model->where('type',2)->where($where)->count();
        $one['tree'] = $this->model->where('type',3)->where($where)->count();
        $one['fore'] = $this->model->where('type',4)->where($where)->count();
        $this->assign('one',$one);
        return $this->fetch();
    }

    public function index()
    {
        if($this->request->param('toid')){
            $this->datas['toid'] = $this->getLoginUser()->id;
            unset($this->datas['uid']);
        }
        $list = $this->model->page_index($this->datas, 'status ASC')
            ->appends($this->request->param());

        $this->assign([
            'list' => $list,
            'num' => $this->model->where($this->datas)->count(),
        ]);
        $this->assign('list', $list);
        $this->assign('_html_empty', '<tr><td colspan=\'100\'>暂无数据</td></tr>');

        return $this->fetch();
    }

    public function edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();

            $data['status'] = 0;
            $data['uid'] = $this->getLoginUser()->id;
            $data['uname'] = $this->getLoginUser()->name;
            $this->model->allowField(true)->save($data);
            if($this->request->isAjax()){
                return ajax_info('留言成功！');
                $this->success('留言成功!');
            }else {
                $this->success('留言成功!');
                return ajax_info('留言成功！');
            }
        }else{
            $list = model('user')->where('type',1)->where('id','neq',$this->getLoginUser()->id)->field('id,name')->select();
            $this->assign('list',$list);
        }
        return $this->fetch();
    }

    public function red($id)
    {
        $data = $this->request->param();
        if ($this->request->isPost()) {
            $this->model->update(['status' => 0], ['id' => $data['id']]);
            $data['fid'] = $data['id'];
            $data['create_time'] = time();
            unset($data['id']);
            db('mailboxmass')->insert($data);
            $this->success('保存成功!');
            return ajax_info('保存成功！');
        }
        $mailbox = $this->model->where('id', $id)->where('uid', $this->getLoginUser()->id)->find();
        $maillist = db('mailboxmass')->where('fid', $mailbox->id)->select();
        $this->assign(
            [
                'data' => $mailbox,
                'list' => $maillist,
            ]
        );
        return $this->fetch();
    }

    /**
     * 领导回复
     * @param $id
     * @return mixed|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function toidread($id)
    {
        $data = $this->request->param();
        if ($this->request->isPost()) {
            $this->model->update(['status' => 1], ['id' => $data['id']]);
            $data['fid'] = $data['id'];
            $data['adminname'] = $this->getLoginUser()->name;
            $data['create_time'] = time();
            unset($data['id']);
            db('mailboxmass')->insert($data);
            $this->success('保存成功!');
            return ajax_info('保存成功！');
        }
        $mailbox = $this->model->where('id', $id)->where('toid', $this->getLoginUser()->id)->find();
        $maillist = db('mailboxmass')->where('fid', $mailbox->id)->select();
        $this->assign(
            [
                'data' => $mailbox,
                'list' => $maillist,
            ]
        );
        return $this->fetch();
    }

}
